package com.chainyoung.component_tmperson.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chainyoung.common.appcommon.entity.HdWalletCache;
import com.chainyoung.common.utils.SPUtil;
import com.chainyoung.component_tmperson.R;
import com.chainyoung.component_tmperson.ui.activity.hdwallet.HdWalletActivity;
import com.chainyoung.component_tmperson.ui.activity.hdwallet.SelectHdWalletListActivity;

import java.math.BigDecimal;
import java.util.List;

import static com.chainyoung.common.constant.LibConstant.WALLET_SAVE_TIME;

public class LocalWalletListAdapter1 extends RecyclerView.Adapter {


    private static final int TYPE_HEAD = 0;
    private static final int TYPE_LIST = 1;

    private List<HdWalletCache> mData;
    private Context mContext;
    private LayoutInflater layoutInflater;

    private boolean isHideRemind;

    public LocalWalletListAdapter1(Context context, List<HdWalletCache> datas, boolean isHideRemind) {
        this.mContext = context;
        this.mData = datas;
        this.isHideRemind = isHideRemind;
        layoutInflater = LayoutInflater.from(mContext);
    }


    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return TYPE_HEAD;
        } else return TYPE_LIST;


    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_HEAD:
                View view = layoutInflater.inflate(R.layout.layout_local_wallet_head, null);
                return new HeadHolder(view);
            case TYPE_LIST:
                view = layoutInflater.inflate(R.layout.tmperson_layout_wallet_item, null);
                return new ListHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE_HEAD:
                HeadHolder headHolder = (HeadHolder) holder;
                headHolder.itemView.setVisibility(isHideRemind ? View.GONE : View.VISIBLE);
                if (isHideRemind) {
                    headHolder.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
                }
                break;
            case TYPE_LIST:
                HdWalletCache item = mData.get(position - 1);
                ListHolder listHolder = (ListHolder) holder;
                listHolder.tvName.setText(item.getWalletName());
                listHolder.tvNum.setText(new BigDecimal(item.getBalance()).setScale(8, BigDecimal.ROUND_DOWN).stripTrailingZeros().toPlainString());
                listHolder.tvCnyAmount.setText(item.getBanlanceCny());
                int logo = R.mipmap.icon_eth;
                listHolder.ivLogo.setImageResource(logo);
                listHolder.rlPurse.setTag(item);
                listHolder.ivMain.setVisibility(item.isMainWallet()?View.VISIBLE:View.GONE);


                break;
        }
    }

    @Override
    public int getItemCount() {
        return mData.size() + 1;
    }

    class HeadHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView tvRemindLater;
        TextView tvBackUp;
        RelativeLayout rlHead;

        public HeadHolder(View itemView) {
            super(itemView);
            tvRemindLater = itemView.findViewById(R.id.tv_remind_later);
            tvBackUp = itemView.findViewById(R.id.tv_goto_backup);
            rlHead = itemView.findViewById(R.id.rl_head);
            tvRemindLater.setOnClickListener(this);
            tvBackUp.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.tv_remind_later) {
                SPUtil.put(WALLET_SAVE_TIME, System.currentTimeMillis());
                isHideRemind = true;
                notifyDataSetChanged();
            } else {
                Intent intent = new Intent(mContext, SelectHdWalletListActivity.class);
                mContext.startActivity(intent);
            }
        }
    }

    class ListHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        TextView tvNum;
        TextView tvCnyAmount;
        ImageView ivLogo;
        ImageView ivMain;

        RelativeLayout rlPurse;


        public ListHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.coin_name);
            tvNum = itemView.findViewById(R.id.coin_num);
            tvCnyAmount = itemView.findViewById(R.id.coin_cny_amount);
            ivLogo = itemView.findViewById(R.id.coin_logo);
            rlPurse = itemView.findViewById(R.id.rl_purse);
            ivMain=itemView.findViewById(R.id.iv_main);
            rlPurse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    HdWalletCache item = (HdWalletCache) v.getTag();
                    Intent intent = new Intent(mContext, HdWalletActivity.class);
                    intent.putExtra("hdwallet", item);
                    mContext.startActivity(intent);
                }
            });
        }
    }


}
